<template>
  <el-dialog :visible="showAssign" title="分配角色" @close="hClose">
    <el-checkbox-group v-model="permIds">
      <el-checkbox v-for="item in list" :key="item.id" :label="item.id">{{ item.name }}</el-checkbox>
    </el-checkbox-group>
    <div style="margin-top: 20px; text-align: right">
      <el-button type="primary" @click="hOK">确 定</el-button>
      <el-button @click="hClose">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { getRole, roleDetail, perRole } from '@/api/role'
export default {
  props: {
    showAssign: {
      type: Boolean,
      default: false
    },
    curId: {
      type: Number,
      required: true
    }
  },
  data() {
    return {
      list: [],
      permIds: [] // 多选框选中的
    }
  },
  created() {
    this.getRole()
  },
  methods: {
    hClose() {
      this.$emit('update:showAssign', false)
    },
    // 获取所有角色
    async getRole() {
      const res = await getRole()
      this.list = res.data
    },
    // 获取角色详情
    async roleDetail() {
      const res = await roleDetail(this.curId)
      this.permIds = res.data.permIds// 复选框回显
    },
    // 保存
    async hOK() {
      await perRole({ id: this.curId, permIds: this.permIds })
      this.$message.success('保存成功')
      this.hClose()
    }
  }
}
</script>

<style>

</style>
